//自已
class Solution {
public:
    int SquareSum(int n)
    {
        int count = 0;
        while (n)
        {
            count += pow(n % 10, 2);
            n /= 10;
        }
        return count;
    }
    bool isHappy(int n) {
        //快慢指针: 相遇时确定成环, 无论是否是快乐数都会成环, 一个环中都是1, 一个都不是1
        int slow=-1, fast=-2; //注意:不能相同,否则无法进循环                   //初始化成负数/0,  防止影响相遇点的判断 --- 错
        while (slow != fast)//注意
        {
            if (slow==-1 && fast==-2)
                slow=fast=n;
            //slow走一步, fast走两步
            slow = SquareSum(slow);
            //cout << slow << " ";
            fast = SquareSum(fast);
            //cout << fast << " ";
            fast = SquareSum(fast);
            //cout << fast << " " << endl;
        }
        if (slow == 1)
            return true;
        else 
            return false;
    }
};

//答案
class Solution {
public:
    //返回n每一位的平方和
    int SquareSum(int n)
    {
        int sum = 0;
        while (n)
        {
            int pos = n % 10;
            sum += pos * pos;
            n /= 10;
        }
        return sum;
    }
    bool isHappy(int n) {
        //快慢指针: 相遇时确定成环, 无论是否是快乐数都会成环, 一个环中都是1, 一个都不是1
        int slow=n, fast=SquareSum(n);//注意:相同时无法进循环; //因为一定会循环, 只要进了循环一定会相遇, 所以fast先走一步也无影响        
        while (slow != fast)//注意
        {
            //slow走一步, fast走两步
            slow = SquareSum(slow);
            fast = SquareSum(SquareSum(fast));
        }
        return slow == 1;
    }
};